/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package progin;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author Jiwandono
 */
public class adminServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected String[] processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String[] a = new String[7];
        for (int i = 0; i < 7; i++) {
            a[i] = null;
        }

        try {
            Class.forName("com.mysql.jdbc.Driver");

            dbConfig dbc = new dbConfig();
            Connection con = DriverManager.getConnection(dbc.url, dbc.username, dbc.password);

            //edit status
            if (request.getParameter("ubah") != null) {
                //out.println("au ah");
                Statement str = con.createStatement();
                ResultSet status_old = str.executeQuery("SELECT type FROM `user` WHERE iduser = " + request.getParameter("ubah") + " LIMIT 0,1");
                status_old.first();
                String status = status_old.getString("type");
                if (status.equalsIgnoreCase("Dosen")) {
                    status = "Mahasiswa";
                } else if (status.equalsIgnoreCase("Mahasiswa")) {
                    status = "Dosen";
                } else {
                    status = "Admin";
                }
                int pr = str.executeUpdate("UPDATE `user` SET type = '" + status + "' WHERE iduser = " + request.getParameter("ubah"));
                //out.println(status+" ah gelap ");
            }

            Statement sts = con.createStatement();
            ResultSet us = sts.executeQuery("SELECT * FROM `user`");

            us.last();
            if (us.getRow() > 0) {
                us.first();
                int it = 1;
                do {
                    a[0] +=
                            "<tr><td width=\"5%\" style=\"text-align:left\" >" + it + "</td>"
                            + "<td width=\"20%\">" + us.getString("username") + "</td>"
                            + "<td width=\"20%\">" + us.getString("type") + "</td>"
                            + "<td width=\"20%\"><form method=\"post\" action=\"adminServlet\">"
                            + "<input type=\"hidden\" name=\"ubah\" value=\"" + us.getString("iduser") + "\"/>"
                            + "<button type=\"submit\">" + "ubahen" + "</button>"
                            + "</form></td></tr>";
                    it++;
                } while (us.next());
            }

            //add fakultas baru
            if (request.getParameter("newfak") != null) {
                //out.println("au ah");
                Statement str = con.createStatement();
                int pr = str.executeUpdate("INSERT INTO `fakultas` (`nama`) VALUES ('" + request.getParameter("newfak") + "')");
                //out.println(str+" ah gelap "+pr);
            }
            //dell fakultas
            if (request.getParameter("hapusfak") != null) {
                //out.println("au ah");
                Statement str = con.createStatement();
                int pr = str.executeUpdate("DELETE FROM `fakultas` WHERE `idfakultas` = " + request.getParameter("hapusfak"));
                //out.println(str+" ah gelap "+pr);
            }
            //edit fakultas
            if (request.getParameter("editfak") != null && request.getParameter("idfak") != null) {
                //out.println("au ah");
                Statement str = con.createStatement();
                int pr = str.executeUpdate("UPDATE `fakultas` SET nama = '" + request.getParameter("editfak") + "' WHERE idfakultas = " + request.getParameter("idfak"));
                //out.println(str+" ah gelap "+pr);
            }

            Statement st = con.createStatement();
            ResultSet fk = st.executeQuery("SELECT * FROM `fakultas`");

            fk.last();
            if (fk.getRow() > 0) {
                fk.first();

                int it = 1;
                do {
                    a[1] += "<tr ><td width=\"5%\" style=\"text-align:left\" >" + it + "</td>"
                            + "<td width=\"20%\">" + fk.getString("nama") + "</td>"
                            + "<td width=\"20%\"><form method=\"post\" action=\"adminServlet\">"
                            + "<input type=\"hidden\" name=\"idfak\" value=\"" + fk.getString("idfakultas") + "\"/>"
                            + "<input type=\"text\" name=\"editfak\" value=\"\"/>"
                            + "</form></td>"
                            + "<td width=\"20%\"><form method=\"post\" action=\"adminServlet\">"
                            + "<input type=\"hidden\" name=\"hapusfak\" value=\"" + fk.getString("idfakultas") + "\"/>"
                            + "<button type=\"submit\">" + "apusen jek" + "</button>"
                            + "</form></td></tr>";
                    it++;
                } while (fk.next());
            }

            //add prodi baru
            if (request.getParameter("newprod") != null) {
                //out.println("au ah");
                Statement str = con.createStatement();
                int pr = str.executeUpdate("INSERT INTO `prodi` (`nama`,`idfakultas`) VALUES ('" + request.getParameter("newprod") + "','" + request.getParameter("fak4prod") + "')");
                //out.println(str+" ah gelap "+pr);
            }
            //dell prodi
            if (request.getParameter("hapusprod") != null) {
                //out.println("au ah");
                Statement str = con.createStatement();
                int pr = str.executeUpdate("DELETE FROM `prodi` WHERE `idprodi` = " + request.getParameter("hapusprod"));
                //out.println(str+" ah gelap "+pr);
            }
            //edit prodi
            if (request.getParameter("editprod") != null && request.getParameter("idprod") != null) {
                //out.println("au ah");
                Statement str = con.createStatement();
                int pr = str.executeUpdate("UPDATE `prodi` SET nama = '" + request.getParameter("editprod") + "' WHERE idprodi = " + request.getParameter("idprod"));
                //out.println(str+" ah gelap "+pr);
            }
            //edit prodi
            if (request.getParameter("ubahprod") != null) {
                //out.println("au ah");
                Statement str = con.createStatement();
                int pr = str.executeUpdate("UPDATE `prodi` SET idfakultas = '" + request.getParameter("fak4prod") + "' WHERE idprodi = " + request.getParameter("ubahprod"));
                //out.println(str+" ah gelap "+pr);
            }


            Statement faklist = con.createStatement();
            ResultSet flist = faklist.executeQuery("SELECT * FROM `fakultas`");

            flist.last();
            if (flist.getRow() > 0) {
                flist.first();

                do {
                    a[3] += "<option value=\"" + flist.getString("idfakultas") + "\">" + flist.getString("nama") + "</option>";

                } while (flist.next());
            }


            Statement str = con.createStatement();
            ResultSet pr = str.executeQuery("SELECT * FROM `prodi`");

            pr.last();
            if (pr.getRow() > 0) {
                pr.first();

                int it = 1;
                do {
                    a[2] += "<tr ><td width=\"5%\" style=\"text-align:left\" >" + it + "</td>"
                            + "<td width=\"20%\">" + pr.getString("nama") + "</td>"
                            + "<td width=\"20%\"><form method=\"post\" action=\"adminServlet\">"
                            + "<input type=\"hidden\" name=\"idprod\" value=\"" + pr.getString("idprodi") + "\"/>"
                            + "<input type=\"text\" name=\"editprod\" value=\"\" size=\"20\"/>"
                            + "</form></td>"
                            + "<td width=\"20%\"><form method=\"post\" action=\"adminServlet\">"
                            + "<input type=\"hidden\" name=\"hapusprod\" value=\"" + pr.getString("idprodi") + "\"/>"
                            + "<button type=\"submit\">" + "apusen jek" + "</button>"
                            + "</form></td></tr>";

                    it++;
                } while (pr.next());
            }


            Statement strr = con.createStatement();
            ResultSet pf = strr.executeQuery("SELECT * FROM `prodi`");

            pf.last();
            if (pf.getRow() > 0) {
                pf.first();

                int it = 1;
                do {
                    Statement stry = con.createStatement();
                    ResultSet flist3 = stry.executeQuery("SELECT * FROM `fakultas` WHERE idfakultas = "+pf.getString("idfakultas")+" LIMIT 0,1");

                    //flist3.last();
                    flist3.first();
                    if (flist3.getRow() != 0) {
                            a[5] = "";
                            a[5] += "<option value=\"" + flist3.getString("idfakultas") + "\">" + flist3.getString("nama") + "</option>";
                    }else{
                        a[5] = "";
                        a[5] += "<option value=\"\">--</option>";
                    }

                    Statement strx = con.createStatement();
                    ResultSet flist2 = strx.executeQuery("SELECT * FROM `fakultas`");

                    flist2.last();
                    if (flist2.getRow() > 0) {
                        flist2.first();

                        do {
                            a[6] += "<option value=\"" + flist2.getString("idfakultas") + "\">" + flist2.getString("nama") + "</option>";

                        } while (flist2.next());
                    }


                    a[4] += "<tr ><td width=\"5%\" style=\"text-align:left\" >" + it + "</td>"
                            + "<td width=\"20%\">" + pf.getString("nama") + "</td>"
                            + "<td width=\"20%\"><form method=\"post\" action=\"adminServlet\">"
                            + "<select name=\"fak4prod\">"
                            + a[5]
                            + a[6]
                            + "</select>"
                            + "</td>"
                            + "<td width=\"20%\">"
                            + "<input type=\"hidden\" name=\"ubahprod\" value=\"" + pf.getString("idprodi") + "\"/>"
                            + "<button type=\"submit\">" + "ubahen" + "</button>"
                            + "</form></td></tr>";

                    it++;a[6]="";
                } while (pf.next());
            }

        } catch (SQLException ex) {
            while (ex != null) {
                out.println("SQL Exception:  " + ex.getMessage());
                ex = ex.getNextException();
            }
        } catch (Exception ex) {
            if (ex != null) {
                out.println("Exception:  " + ex.getMessage());
            }
        }

        //out.println(a);

        return a;
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        String[] adminpage = processRequest(request, response);
        request.setAttribute("user", (adminpage)[0]);
        request.setAttribute("fak", (adminpage)[1]);
        request.setAttribute("prd", (adminpage)[2]);
        request.setAttribute("prodbaru", (adminpage)[3]);
        request.setAttribute("prdfak", (adminpage)[4]);

        getServletConfig().getServletContext().getRequestDispatcher("/admin.jsp").forward(request, response);

    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        PrintWriter out = response.getWriter();
        String[] adminpage = processRequest(request, response);
        request.setAttribute("user", (adminpage)[0]);
        request.setAttribute("fak", (adminpage)[1]);
        request.setAttribute("prd", (adminpage)[2]);
        request.setAttribute("prodbaru", (adminpage)[3]);
        request.setAttribute("prdfak", (adminpage)[4]);

        getServletConfig().getServletContext().getRequestDispatcher("/admin.jsp").forward(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
